﻿using System;
using System.Linq;

namespace ALDIOrganizer
{
    public partial class Cell
    {
        [NonSerialized]
        private string _value;
        public string Value
        {
            get
            {
                return _value;
            }
            set
            {
                _value = value;
                GetParent.TriggerOnCellValueChangedEvent(this);
            }
        }

        private Position _roleValue;
        public Position RoleValue
        {
            get
            {
                return _roleValue;
            }
            set
            {
                _roleValue = value;
                foreach (var shift in Shifts.ToArray())
                {
                    shift.JobPosition = value;
                }
            }
        }

        void UpdateValue()
        {
            if (MyCellType == CellType.TotalHours)
            {
                return; // BottomBar'pShift Cells (of Type - Total) don't use this method. They get updated elsewhere.
            }
            var valueInPreparation = String.Empty;

            EmployeeInRota employee = null;
            if (GetParentMode == SheetType.Rota)
            {
                employee = GetMyEmployee;
            }

            var hadSomeShifts = false;
            #region first column
            if (_column == 0)
            {
                switch (GetParentMode)
                {
                    case SheetType.Rota:
                        {
                            valueInPreparation = employee.ToString();
                            MyCellType = CellType.Employee;
                            break;
                        }

                    case SheetType.Template:
                        {
                            MyCellType = CellType.JobPosition;
                            break;
                        }
                }
            }
            #endregion
            #region other columns
            else
            {
                if (HasAnyShifts)
                {
                    MyCellType = CellType.Shift;
                    hadSomeShifts = true;
                    valueInPreparation = Shift.ToString(Shifts, (GetParentMode == SheetType.Template));
                }
                else
                {
                    MyCellType = CellType.Blank;
                }
            }
            #endregion

            if (GetParentMode == SheetType.Rota &&
                _column > 0)
            {
                /* The order is important - first we are "colouring" requests, but they are overriden
                 * by holidays (if an _employee is On holidays, then we don't care if he cannot work On Wednesday
                 * because of college (Request) etc. - _holiday overrides it.
                 * If the _employee is not employed at all, then we don't care about neither requests nor holidays
                 * in that period */

                if (employee.RequestStatuses.Key[_column - 1])
                {
                    _statusID = (from _id in employee.RequestStatuses.Value[_column - 1]
                                 select _id.ID).ToArray();
                    valueInPreparation = Shift.ToString(
                        Shifts,
                        (GetParentMode == SheetType.Template));
                }
                if (employee.HolidayStatuses.Key[_column - 1])
                {
                    MyCellType = CellType.Holiday;                    
                    valueInPreparation = employee.HolidayStatuses.Value[_column - 1].ToShortString();
                    _statusID = new[]
                                {
                                    employee.HolidayStatuses.Value[_column - 1].ID
                                };
                }
                if ((employee.ContractStatuses.Value[_column - 1] == null))
                {
                    MyCellType = CellType.Dead;
                }
                else
                {
                    if (!(employee.ContractStatuses.Value[_column - 1].Hired))
                    {
                        MyCellType = CellType.Dead;
                    }
                }
                if (MyCellType == CellType.Dead)
                {
                    valueInPreparation = String.Empty;
                }
                /* if at the start of the method the cell DID contain some shifts - but we now "realized"
                 * that */
                if (hadSomeShifts &
                    MyCellType != CellType.Shift)
                {
                    ClearShifts();
                }
            }

            #region common
            if (HasAnyRequests)
            {
                foreach (var _request in GetMyRequests)
                {
                    valueInPreparation += Environment.NewLine +
                        _request;                    
                }
            }
            if (HasAnyNotes)
            {
                valueInPreparation += Environment.NewLine + "*" + Notes;
            }

            // trimming
            if (valueInPreparation.Length > Settings.Default.CELL_TrimContentsAt)
            {
                valueInPreparation = valueInPreparation.Substring(
                    0,
                    Settings.Default.CELL_TrimContentsAt - 7) +
                    " [...]";
            }
            #endregion

            Value = valueInPreparation;
        }
    }
}
